/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import static controller.PessoaDAO.getInstance;
import java.awt.HeadlessException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import models.Cliente;
import utils.conexao.DataBase;
import utils.conexao.DataBaseFactory;
import utils.observer.entity.EntitySubject;

/**
 *
 * @author Thiago Lippel
 */
public class ClienteDAO {
    
        private static ClienteDAO instance;
        private EntitySubject subject;
        
        private ClienteDAO(){
             this.subject = new EntitySubject();
        }
        
        public static ClienteDAO getInstance(){
            if(instance == null){
                instance = new ClienteDAO();
            }            
            return instance;
        }
        
        public EntitySubject getSubject() {
            return this.subject;
        }
    
        public static void inserir(Cliente c) {
        try {
            if (!DataBaseFactory.getConexao().persistir(c)) {
                JOptionPane.showMessageDialog(null, "Não foi possível inserir o Cliente.");
            }
            getInstance().subject.notifyObservers();
        } catch (IllegalArgumentException | IllegalAccessException | DataBase.DataBaseIDException | DataBase.DataBaseDuplicateIDDeclarationException | HeadlessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public static void alterar(Cliente c) {
        try {
            if (!DataBaseFactory.getConexao().persistir(c)) {
                JOptionPane.showMessageDialog(null, "Não foi possível alterar Cliente.");
            }
            getInstance().subject.notifyObservers();
        } catch (IllegalArgumentException | IllegalAccessException | DataBase.DataBaseIDException | DataBase.DataBaseDuplicateIDDeclarationException | HeadlessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public static void excluir(Cliente c) {
        try {
            DataBaseFactory.getConexao().excluir(c);
            
            getInstance().getSubject().notifyObservers();
        } catch (DataBase.DataBaseDuplicateIDDeclarationException | DataBase.DataBaseIDException | IllegalArgumentException | IllegalAccessException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
    
    public static ArrayList<Cliente> getClientes() {
        return DataBaseFactory.getConexao().getAll(Cliente.class);
    }
   
}
